package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@RequiredArgsConstructor
public class SetmealController {

    private final SetmealService setmealService;

    private final SetmealDishService setmealDishService;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result saveWithDish(@RequestBody SetmealDTO setmealDTO) {

        setmealService.saveWithDish(setmealDTO);

        return Result.success();
    }


    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @Transactional
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmealById(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改菜品:{}", setmealDTO);
        setmealService.updateWithSetmealDish(setmealDTO);

        return Result.success();
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status, id);

        return Result.success();
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询套餐")
    public Result<Setmeal> getEmployeeById(@ApiParam("套餐id") @PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        return Result.success(setmeal);
    }

    /**
     * 套餐批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("套餐批量删除")
    @Transactional
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("菜品批量删除{}", ids);

        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            //判断当前套餐是否能删除--是否存在起售中的套餐？
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //当前套餐处于起售中,不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //删除套餐表中的套餐数据
        setmealService.removeByIds(ids);

        for (Long id : ids) {
            //要删除的套餐菜品关系表的数据
            List<SetmealDish> setmealDishes = setmealDishService.lambdaQuery().eq(SetmealDish::getSetmealId, id).list();

            for (SetmealDish setmealDish : setmealDishes) {
                //删除套餐菜品表的菜单数据
                setmealDishService.removeById(setmealDish);
            }

        }


        return Result.success();
    }


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("菜品分页查询:{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

}
